package com.changhongit.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.changhongit.dao.ServiceProviderDao;
import com.changhongit.entity.ServiceProject;
import com.changhongit.entity.ServiceProvider;
import com.changhongit.entity.ServiceType;
import com.changhongit.entity.ServiceUser;
import com.changhongit.entity.User;
import com.changhongit.service.ServiceProjectService;
import com.changhongit.service.ServiceProviderService;
import com.changhongit.service.ServiceTypeService;
import com.changhongit.service.ServiceUserService;
import com.changhongit.util.Common;
import com.changhongit.util.Escape;
import com.changhongit.util.PageView;

/**
 * 
 * @author  JacksonYang
 * 2014-2-13
 * @Email: 
 * @version 1.0v
 */
@Controller
@RequestMapping("/background/serviceProvider/")
public class ServiceProviderController {
	@Autowired
	private ServiceProviderDao serviceProviderService;
	
	@Autowired
	private ServiceProviderService serviceProviderS;
	
	@Autowired
	private ServiceTypeService serviceTypeService;
	
	@Autowired
	private ServiceUserService serviceUserService;
	
	@Autowired
	private ServiceProjectService serviceProjectService;
	/**
	 * @param model
	 * 存放返回界面的model
	 * @return
	 */
	@RequestMapping("query")
	public String query(Model model, ServiceProvider serviceProvider, String pageNow,HttpServletRequest request) {
		PageView pageView = null;
		if (Common.isEmpty(pageNow)) {
			pageView = new PageView(1);
		} else {
			pageView = new PageView(Integer.parseInt(pageNow));
		}
		User user = (User) request.getSession().getAttribute("userSession");
		serviceProvider.setOperatorname(user.getUserName());;
		List<ServiceType> serviceTypes = serviceTypeService.findByPid(0);
		if(serviceProvider.getPtypeId()!=0){
			List<ServiceType> userviceTypes = serviceTypeService.findByPid(serviceProvider.getPtypeId());
			model.addAttribute("userviceTypes", userviceTypes);
		}
		pageView = serviceProviderS.query(pageView, serviceProvider);
		model.addAttribute("pageView", pageView);
		model.addAttribute("serviceTypes", serviceTypes);
		model.addAttribute("serviceProvider", serviceProvider);
		return "/background/serviceProvider/list";
	}

	/**
	 * 保存数据
	 * 
	 * @param model
	 * @param videoType
	 * @return
	 */
	@RequestMapping("add")
	public String add(Model model, ServiceProvider serviceProvider,HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("userSession");
		serviceProvider.setUserId(user.getUserId());
		serviceProvider.setOperatorname(user.getUserName());
		serviceProvider.setServicestatus("Y");
		serviceProviderService.add(serviceProvider);
		return "redirect:query.html";
	}

	/**
	 * 跑到新增界面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("addUI")
	public String addUI() {
		return "/background/serviceProvider/add";
	}
	

	/**
	 * 跑到新增界面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("addservice")
	public String addservice(Model model,ServiceProvider serviceProvider) {
		serviceProvider.setAddress(Escape.unescape(serviceProvider.getAddress()));
		model.addAttribute("lat", serviceProvider.getLat());
		model.addAttribute("lon", serviceProvider.getLon());
		model.addAttribute("address", serviceProvider.getAddress());
		List<ServiceType> servicetypes = serviceTypeService.findByPid(0);
		model.addAttribute("servicetypes", servicetypes);
		return "/background/serviceProvider/addservice";
	}
	
	@RequestMapping(value = "/getTypeList")
	public void preCheckUserName( String pid,
			HttpServletResponse response) {
		response.setContentType("text/html;charset=GBK");
		try {
			String checkResult = null;
			List<ServiceType> serviceTypes = serviceTypeService.findByPid(Integer.valueOf(pid));
			checkResult = "<select id=\"type_Id\" name=\"typeId\" style=\"height: 20px;width: 200px\">";
			String context = null;
			for (int i = 0; i < serviceTypes.size(); i++) {
				ServiceType serviceType = serviceTypes.get(i);
				if (context == null) {
					context = "<option value=" + serviceType.getId()
							+ ">" + serviceType.getName() + "</option>";
				} else {
					context = context + "<option value="
							+ serviceType.getId() + ">"
							+ serviceType.getName() + "</option>";
				}
			}
			checkResult = checkResult + context + "</select>";
			response.getWriter().print(checkResult);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/searchTypeList")
	public void searchTypeList( String pid,
			HttpServletResponse response) {
		response.setContentType("text/html;charset=GBK");
		try {
			String checkResult = null;
			List<ServiceType> serviceTypes = serviceTypeService.findByPid(Integer.valueOf(pid));
			checkResult = "<select id=\"type_Id\" name=\"typeId\"><option value=\"0\">请选择</option>";
			String context = null;
			for (int i = 0; i < serviceTypes.size(); i++) {
				ServiceType serviceType = serviceTypes.get(i);
				if (context == null) {
					context = "<option value=" + serviceType.getId()
							+ ">" + serviceType.getName() + "</option>";
				} else {
					context = context + "<option value="
							+ serviceType.getId() + ">"
							+ serviceType.getName() + "</option>";
				}
			}
			checkResult = checkResult + context + "</select>";
			response.getWriter().print(checkResult);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除
	 * 
	 * @param model
	 * @param videoTypeId
	 * @return
	 */
	@RequestMapping("deleteById")
	public String deleteById(Model model, String id) {
		serviceProviderService.delete(id);
		return "redirect:query.html";
	}

	/**
	 * 修改界面
	 * 
	 * @param model
	 * @param videoTypeIds
	 * @return
	 */
	@RequestMapping("getById")
	public String getById(Model model, String id) {
		ServiceProvider serviceProvider = serviceProviderService.getById(id);
		List<ServiceType> uservicetypes = serviceTypeService.findByPid(0);
		List<ServiceType> servicetypes = serviceTypeService.findByPid(serviceProvider.getPtypeId());
		model.addAttribute("uservicetypes", uservicetypes);
		model.addAttribute("servicetypes", servicetypes);
		model.addAttribute("serviceProvider", serviceProvider);
		return "/background/serviceProvider/edit";
	}
	
	@RequestMapping("getInfo")
	public String getInfo(Model model, String id) {
		ServiceProvider serviceProvider = serviceProviderService.getById(id);
		List<ServiceType> uservicetypes = serviceTypeService.findByPid(0);
		List<ServiceType> servicetypes = serviceTypeService.findByPid(serviceProvider.getPtypeId());
		model.addAttribute("uservicetypes", uservicetypes);
		model.addAttribute("servicetypes", servicetypes);
		model.addAttribute("serviceProvider", serviceProvider);
		return "/background/serviceProvider/show";
	}

	/**
	 * 更新类型
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("update")
	public String update(Model model, ServiceProvider serviceProvider) {
		serviceProviderService.modify(serviceProvider);
		if(serviceProvider.getId()==null)
			serviceProviderService.saveServiceProvider(serviceProvider);
		return "redirect:query.html";
	}
	
	/**
	 * 更新类型
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("updatePassword")
	public String updatePassword(Model model, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("userSession");
		String username = user.getUserName();
		ServiceProvider serviceProvider = serviceProviderS.findOneServiceProvider(username);
		model.addAttribute("serviceProvider", serviceProvider);
		return "/background/serviceProvider/editpassword";
	}
	
	
	/**
	 * 更新类型
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("updateStatus")
	public String updateStatus(Model model, ServiceProvider serviceProvider) {
		serviceProviderService.updateFlags(serviceProvider);
		return "redirect:query.html";
	}
	
	/**
	 * 更新类型
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("seeserviceuserandproject")
	public String seeServiceUserAndProject(Model model, ServiceProvider serviceProvider,String type) {
		if("user".equals(type)){
			PageView pageView = new PageView(1);
			ServiceUser serviceUser = new ServiceUser();
			serviceUser.setSproviderid(serviceProvider.getId());
			pageView = serviceUserService.query(pageView, serviceUser);
			model.addAttribute("pageView", pageView);
			return "/background/serviceProvider/user/list";
		}else if("project".equals(type)){
			PageView pageView = new PageView(1);
			ServiceProject serviceProject = new ServiceProject();
			serviceProject.setSproviderid(serviceProvider.getId());
			pageView = serviceProjectService.query(pageView, serviceProject);
			model.addAttribute("pageView", pageView);
			return "/background/serviceProvider/project/list";
		}
		return "";
	}

	/**
	 * 删除所选的
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("deleteAll")
	public String deleteAll(Model model, String[] check) {
		for (String string : check) {
			serviceProviderService.delete(string);
		}
		return "redirect:query.html";
	}
	
	@RequestMapping(value = "/checkpassword")
	public void checkpassword( String id,String oldpassword,
			HttpServletResponse response) {
		response.setContentType("text/html;charset=GBK");
		try {
			String checkResult = null;
			ServiceProvider serviceProvider = serviceProviderService.getById(id);
			if(serviceProvider.getPassword().equals(oldpassword)){
				checkResult = "0";
			}else{
				checkResult = "1";
			}
			response.getWriter().print(checkResult);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/updatePasswordInfo")
	public void updatePasswordInfo( Model model,int id,String newpassword,
			HttpServletResponse response) {
		response.setContentType("text/html;charset=GBK");
		try {
			String checkResult = "0";
			ServiceProvider sp = new ServiceProvider();
			sp.setId(id);
			sp.setPassword(newpassword);
			serviceProviderService.modify(sp);
			response.getWriter().print(checkResult);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/checkUserName")
	public void checkUserName(String username,
			HttpServletResponse response) {
		response.setContentType("text/html;charset=GBK");
		try {
			String checkResult = null;
			username = Escape.unescape(username);
			ServiceProvider serviceProvider = serviceProviderS.findOneServiceProviderByUserName(username);
			if(serviceProvider!=null){
				checkResult = "0";
			}else{
				checkResult = "1";
			}
			response.getWriter().print(checkResult);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
}